package com.qf.controller;

import com.qf.dto.TProductSearchDTO;
import com.qf.entity.TProduct;
import com.qf.mapper.TProductMapper;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LiuLiang
 * @Date 2020/3/14
 */
@Controller
//@RequestMapping("/search")
public class SearchController {

    @Autowired
    private TProductMapper productMapper;


    @Autowired
    private SolrClient solrClient;

    @RequestMapping("/search")
    @ResponseBody
    public List<TProductSearchDTO> searchGoodsByKeywords(String keywords, Integer pageNum, Integer pagesize) {
        SolrQuery query = new SolrQuery();
        //设置查询参数
        //String keyword = "牙膏";
        query.setQuery(keywords);
        //复制域
        query.set("df","t_item_keywords");
        //分页

        System.out.println(pageNum);
        System.out.println(pagesize);
        query.setStart((pageNum-1)*pagesize);
        query.setRows(pagesize);
        //高亮
        query.setHighlight(true);//开启高亮
        query.addHighlightField("t_pname");
        query.setHighlightSimplePre("<span style='color:red'>");
        query.setHighlightSimplePost("</span>");
        //执行查询，得到结果集
        List<TProductSearchDTO> productSearchDTOList = new ArrayList<>();
        try {
            QueryResponse response = solrClient.query(query);

            SolrDocumentList results = response.getResults();
            Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();


            for (SolrDocument doc : results) {
                TProductSearchDTO product = new TProductSearchDTO();
                String id_str = (String) doc.getFieldValue("id");
                Long id = Long.parseLong(id_str);
                product.setPid(id);
                //高亮的商品名称的封装
                Map<String, List<String>> stringListMap = highlighting.get(id_str);
                List<String> t_pnames = stringListMap.get("t_pname");
                //if (t_pnames != null){
                    String t_pname = t_pnames.get(0);
                    product.setPname(t_pname);
//                }else {
//                    String t_pname = (String) doc.getFieldValue("t_pname");
//                    product.setPname(t_pname);
//                }

//                List<String> t_goods_pdescs = stringListMap.get("t_goods_pdesc");
//                if (t_goods_pdescs!=null){
//                    String t_goods_pdesc = t_goods_pdescs.get(0);
//                    goods.settGoodsPdesc(t_goods_pdesc);
//                }else {
//                    String t_goods_pdesc = (String) doc.getFieldValue("t_goods_pdesc");
//                    goods.settGoodsPdesc(t_goods_pdesc);
//                }
                //高亮end

                Double t_sale_price = (Double) doc.getFieldValue("t_sale_price");
                product.setSalePrice(new BigDecimal(t_sale_price));

                String t_pimage = (String) doc.getFieldValue("t_pimage");
                product.setPimage(t_pimage);

                Long price = (Long) doc.getFieldValue("t_price");
                product.setPid(price);

                Long type_id = (Long) doc.getFieldValue("t_type_id");
                product.setPid(type_id);

                productSearchDTOList.add(product);
            }
        } catch (SolrServerException|IOException e) {
            e.printStackTrace();
        }

        return productSearchDTOList;
    }


    @RequestMapping("/initData")
    @ResponseBody
    public String initDataToSolr() {
        List<TProduct> productList = productMapper.selectAll();
        //存放所有的doc集合
        List<SolrInputDocument> docs = new ArrayList<>();
        for (TProduct product:productList){
            SolrInputDocument doc = new SolrInputDocument();
            doc.setField("id",product.getPid());
            doc.setField("t_pname",product.getPname());
            doc.setField("t_sale_price",product.getSalePrice().floatValue());
            doc.setField("t_pimage",product.getPimage());
            doc.setField("t_price",product.getPrice());
            doc.setField("t_type_id",product.getPrice());
            System.out.println(product);
            docs.add(doc);
        }
        try {
            //solrClient.deleteByQuery("*:*");
            solrClient.add(docs);
            solrClient.commit();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }


}
